package com.com.hefei.wdp.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.SubscribableChannel;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Locale;
@Slf4j
@Component
@EnableBinding(Sink.class)
public class ReceiveMessageListenerController {
    @Autowired
    @Qualifier(Sink.INPUT)
    private SubscribableChannel subscribableChannel;
    @Autowired
    private Sink sink;

    //当bean注入完成后调用,以下三中方式用一种即可
    //第一种方式
    @PostConstruct
    public void init() {
        subscribableChannel.subscribe(new MessageHandler() {
            @Override
            public void handleMessage(Message<?> message) throws MessagingException {

                Object payload = message.getPayload();
                System.out.println(new String((byte[]) payload));
                log.info("------第一种------"+ new String((byte[]) payload));
            }
        });

    }
    //第二种方式
    @ServiceActivator(inputChannel = Sink.INPUT)
    public void onMessage(Object message) {
        System.out.println("@ServiceActivator-->" +message);
        log.info("------第二种------"+ message);

    }
    //第三种方式
    @StreamListener(Sink.INPUT)
    public void onMessage(String message) {
        System.out.println("@StreamListener-->" + message);
        log.info("------第三种------"+ message);

    }
}
